package cc.fs.zhihuapp.rest.asynchttp;

import android.content.Context;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;

/**
 * Created by fostion on 2015/9/9.
 */
public class ApiHelper {
    public static AsyncHttpClient client;

    public static void init(){
        if(client == null){
            client = new AsyncHttpClient();
        }
    }

    public static void get(String url, ApiHandler handler) {
        client.get(url, handler);
    }

    public static void post(String url, ApiHandler handler) {
        client.post(url, handler);
    }

    public static void post(String url,String key,byte[] byteArray, ApiHandler handler) {
        RequestParams requestParams = new RequestParams();
        requestParams.put(key,new ByteArrayInputStream(byteArray));
        client.post(url,requestParams , handler);
    }

    public static void post(String url, HashMap<String, String> params, ApiHandler handler) {
        RequestParams requestParams = new RequestParams(params);
        client.post(url, requestParams, handler);
    }

    public static void post(String url, String key, String filePath, ApiHandler handler) throws FileNotFoundException {
        RequestParams requestParams = new RequestParams();
        requestParams.put(key, new File(filePath));
        client.post(url, requestParams, handler);
    }

    public static void put(String url, ApiHandler handler) {
        client.put(url, handler);
    }

    public static void put(String url, HashMap<String, String> params, ApiHandler handler) {
        RequestParams requestParams = new RequestParams(params);
        client.put(url, requestParams, handler);
    }

    public static void put(String url, String key, byte[] byteArray, ApiHandler handler) {
        RequestParams requestParams = new RequestParams();
        requestParams.put(key, new ByteArrayInputStream(byteArray));
        client.put(url, requestParams, handler);
    }

    public static void put(String url, String key, String filePath, ApiHandler handler) throws FileNotFoundException {
        RequestParams requestParams = new RequestParams();
        requestParams.put(key, new File(filePath));
        client.put(url, requestParams, handler);
    }

    public static void delete(String url, ApiHandler handler) {
        client.delete(url, handler);
    }

    public static void delete(String url, HashMap<String, String> params, ApiHandler handler) {
        RequestParams requestParams = new RequestParams(params);
        client.delete(url, requestParams, handler);
    }

    public static void setHead(String token,String mobile){
        client.addHeader("Authorization", String.format("Token token=%s, mobile=%s",token,mobile));
    }

    /**
     *  使用系统的持久持久cookie
     */
    public static void setPersistentCookie(Context context){
        PersistentCookieStore persistentCookieStore = new PersistentCookieStore(context);
        client.setCookieStore(persistentCookieStore);
    }

    public static void cancleAll(Context context){
        client.cancelRequests(context, true);
    }

}
